package mylab.utils.common;

import java.util.Arrays;
import java.util.stream.DoubleStream;
import java.util.stream.IntStream;
import java.util.stream.LongStream;

import lombok.experimental.UtilityClass;

@UtilityClass
public class ArrayConversionUtil {

    // --- Byte 数组转换 ---

    public byte[] toPrimitive(Byte[] wrapperArray) {
        if (wrapperArray == null) return null;
        byte[] result = new byte[wrapperArray.length];
        for (int i = 0; i < wrapperArray.length; i++) {
            result[i] = wrapperArray[i]; // 自动拆箱
        }
        return result;
    }

    public Byte[] toWrapper(byte[] primitiveArray) {
        if (primitiveArray == null) return null;
        Byte[] result = new Byte[primitiveArray.length];
        for (int i = 0; i < primitiveArray.length; i++) {
            result[i] = primitiveArray[i]; // 自动装箱
        }
        return result;
    }

    // --- Integer 数组转换 ---

    public int[] toPrimitive(Integer[] wrapperArray) {
        if (wrapperArray == null) return null;
        return Arrays.stream(wrapperArray)
                .mapToInt(Integer::intValue)
                .toArray();
    }

    public Integer[] toWrapper(int[] primitiveArray) {
        if (primitiveArray == null) return null;
        return IntStream.of(primitiveArray)
                .boxed()
                .toArray(Integer[]::new);
    }

    // --- Long 数组转换 ---

    public long[] toPrimitive(Long[] wrapperArray) {
        if (wrapperArray == null) return null;
        return Arrays.stream(wrapperArray)
                .mapToLong(Long::longValue)
                .toArray();
    }

    public Long[] toWrapper(long[] primitiveArray) {
        if (primitiveArray == null) return null;
        return LongStream.of(primitiveArray)
                .boxed()
                .toArray(Long[]::new);
    }

    // --- Double 数组转换 ---

    public double[] toPrimitive(Double[] wrapperArray) {
        if (wrapperArray == null) return null;
        return Arrays.stream(wrapperArray)
                .mapToDouble(Double::doubleValue)
                .toArray();
    }

    public Double[] toWrapper(double[] primitiveArray) {
        if (primitiveArray == null) return null;
        return DoubleStream.of(primitiveArray)
                .boxed()
                .toArray(Double[]::new);
    }

    // --- Boolean 数组转换 ---

    public boolean[] toPrimitive(Boolean[] wrapperArray) {
        if (wrapperArray == null) return null;
        boolean[] result = new boolean[wrapperArray.length];
        for (int i = 0; i < wrapperArray.length; i++) {
            result[i] = wrapperArray[i];
        }
        return result;
    }

    public Boolean[] toWrapper(boolean[] primitiveArray) {
        if (primitiveArray == null) return null;
        Boolean[] result = new Boolean[primitiveArray.length];
        for (int i = 0; i < primitiveArray.length; i++) {
            result[i] = primitiveArray[i];
        }
        return result;
    }
}